const mongoose = require("mongoose")

let ordersModel = mongoose.model("ordersModel")

// 获取订单
module.exports.getOrders = async function (){
    const data = await ordersModel.find().populate('shopsId')
    return data
}

module.exports.getOrdersBypage=async function({pageSize,currentPage,state}){
   
 // 获取当前集合的数据总条数
 const total = (await ordersModel.find({state})).length;
 // rows 获取的是分页的商品数据 .populate('shopsId')多表关联
 const rows = await ordersModel
     .find({state})
     .populate('shopsId')
    //  .populate('storesId')
     .limit(pageSize - 0)   // limit(number) 用于设置请求数据的条数
     .skip((currentPage - 1) * pageSize);   // 用于设置跳过多少条数据

 return { total, rows }
 }

module.exports.updateOrders = async function ({_id,num,price,address,state}){
    const data = await ordersModel.updateOne({_id},{num,price,address,state})
    return data
}

// module.exports.deletOrders = async function (_id){
//     const data = await ordersModel.find(_id)
//     return data
// }

module.exports.addOrders = async function (user){
    const data = await ordersModel.create(user)
    return data
}